package com.lanmao.emc.utils;

import com.lanmao.emc.function.SFunction;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

import java.util.Optional;

/**
 * 描述：
 *
 * @author lanmao
 * @since 2024年11月12日 20:41
 */
public class QueryWrapper<T> {

    private final BoolQueryBuilder boolQueryBuilder;

    public BoolQueryBuilder build() {
        return this.boolQueryBuilder;
    }

    public QueryWrapper() {
        this.boolQueryBuilder = QueryBuilders.boolQuery();
    }

    public static <T> QueryWrapper<T> builder() {
        return new QueryWrapper<>();
    }


    public QueryWrapper<T> term(String field, Object value) {
        Optional.ofNullable(value).ifPresent(x->boolQueryBuilder.must(QueryBuilders.termQuery(field, x)));
        return this;
    }

    public QueryWrapper<T> term(SFunction<T,?> column, Object value) {
        return term(FieldUtils.val(column),value);
    }

    public QueryWrapper<T> filter(String field, Object value) {
        Optional.ofNullable(value).ifPresent(x->boolQueryBuilder.filter(QueryBuilders.termQuery(field, x)));
        return this;
    }

    public QueryWrapper<T> filter(SFunction<T,?> column, Object value) {
        return filter(FieldUtils.val(column),value);
    }

    public QueryWrapper<T> terms(String field, String... value) {
        Optional.ofNullable(value).ifPresent(x->boolQueryBuilder.must(QueryBuilders.termsQuery(field, x)));
        return this;
    }

    public QueryWrapper<T> terms(SFunction<T,?> column, String... value) {
        return terms(FieldUtils.val(column),value);
    }


}